package com.tech.distribute_id;

import com.tech.service.common.DistributeIdService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author litianlin
 * @date 2023/11/25 17:30
 * @description:
 */
@Slf4j
@DubboService
public class DistributeIdServiceImpl implements DistributeIdService {

    @Resource
    private Map<String, AbstractDistributeIdStrategy> map;

    @PostConstruct
    public void init(){
        map = map.values()
                .stream()
                .collect(Collectors.toMap(e->e.getStrategyType().name(), Function.identity()));
    }

    @Override
    public String dbDistributeId() {
        return doGet(StrategyTypeEnum.DB);
    }

    private String doGet(StrategyTypeEnum type){
        try {
            return map.get(type.name()).getDistributeId();
        } catch (Exception e) {
            log.error("doGet error. ", e);
        }
        return null;
    }

    @Override
    public String uuidDistributeId() {
        return doGet(StrategyTypeEnum.UUID);
    }

    @Override
    public String zkDistributeId() {
        return doGet(StrategyTypeEnum.ZK);
    }

    @Override
    public String redisDistributeId() {
        return doGet(StrategyTypeEnum.REIDS);
    }

    @Override
    public String snowDistributeId() {

        return doGet(StrategyTypeEnum.SNOW_FLAKE);
    }
}
